#!/usr/bin/env python
# coding=utf-8
"""
将stanford neural parser解析文本得到的dependency graph利用graphviz输出成png格式的图
"""
import pygraphviz as pgv


# origin_data = [((u'allowed', u'VBD'), u'nsubj', (u'overflow', u'NN')),
# ((u'overflow', u'NN'), u'det', (u'An', u'DT')),
# ((u'overflow', u'NN'), u'compound', (u'integer', u'NN')),
# ((u'overflow', u'NN'), u'nmod', (u'FFmpeg', u'NNP')),
# ((u'FFmpeg', u'NNP'), u'case', (u'in', u'IN')),
# ((u'overflow', u'NN'), u'nmod', (u'Chrome', u'NNP')),
# ((u'Chrome', u'NNP'), u'case', (u'in', u'IN')),
# ((u'Chrome', u'NNP'), u'compound', (u'Google', u'NNP')),
# ((u'overflow', u'NN'), u'acl', (u'57.0.2987.98', u'CD')),
# ((u'57.0.2987.98', u'CD'), u'advmod', (u'prior', u'RB')),
# ((u'57.0.2987.98', u'CD'), u'case', (u'to', u'TO')),
# ((u'57.0.2987.98', u'CD'), u'nmod', (u'Mac', u'NNP')),
# ((u'Mac', u'NNP'), u'case', (u'for', u'IN')),
# ((u'Mac', u'NNP'), u'punct', (u',', u',')),
# ((u'Mac', u'NNP'), u'appos', (u'Windows', u'NNP')),
# ((u'Mac', u'NNP'), u'punct', (u',', u',')),
# ((u'Mac', u'NNP'), u'cc', (u'and', u'CC')),
# ((u'Mac', u'NNP'), u'conj', (u'Linux', u'NNP')),
# ((u'Linux', u'NNP'), u'cc', (u'and', u'CC')),
# ((u'Linux', u'NNP'), u'conj', (u'57.0.2987.108', u'CD')),
# ((u'57.0.2987.98', u'CD'), u'nmod', (u'Android', u'NNP')),
# ((u'Android', u'NNP'), u'case', (u'for', u'IN')),
# ((u'allowed', u'VBD'), u'dobj', (u'attacker', u'NN')),
# ((u'attacker', u'NN'), u'det', (u'a', u'DT')),
# ((u'attacker', u'NN'), u'amod', (u'remote', u'JJ')),
# ((u'allowed', u'VBD'), u'xcomp', (u'perform', u'VB')),
# ((u'perform', u'VB'), u'mark', (u'to', u'TO')),
# ((u'perform', u'VB'), u'dobj', (u'write', u'VB')),
# ((u'write', u'VB'), u'det', (u'an', u'DT')),
# ((u'write', u'VB'), u'nmod', (u'memory', u'NN')),
# ((u'memory', u'NN'), u'case', (u'out', u'IN')),
# ((u'memory', u'NN'), u'case', (u'of', u'IN')),
# ((u'memory', u'NN'), u'compound', (u'bounds', u'NNS')),
# ((u'perform', u'VB'), u'nmod', (u'file', u'NN')),
# ((u'file', u'NN'), u'case', (u'via', u'IN')),
# ((u'file', u'NN'), u'det', (u'a', u'DT')),
# ((u'file', u'NN'), u'amod', (u'crafted', u'VBN')),
# ((u'file', u'NN'), u'compound', (u'video', u'NN')),
# ((u'file', u'NN'), u'punct', (u',', u',')),
# ((u'file', u'NN'), u'amod', (u'related', u'JJ')),
# ((u'related', u'JJ'), u'nmod', (u'ChunkDemuxer', u'NNP')),
# ((u'ChunkDemuxer', u'NNP'), u'case', (u'to', u'TO')),
# ((u'allowed', u'VBD'), u'punct', (u'.', u'.'))]


# 可以使用这几行代码去远程调用920b上的parse服务
# text = "Stack-based buffer overflow in the split_redraw function in split.c in mtr before 0.73, when invoked with the -p (aka --split) option, allows remote attackers to execute arbitrary code via a crafted DNS PTR record. NOTE: it could be argued that this is a vulnerability in the ns_name_ntop function in resolv/ns_name.c in glibc and the proper fix should be in glibc; if so, then this should not be treated as a vulnerability in mtr."
# text = "An issue was discovered on Mimosa Client Radios before 2.2.4 and Mimosa Backhaul Radios before 2.2.4. On the backend of the device's web interface, there are some diagnostic tests available that are not displayed on the webpage; these are only accessible by crafting a POST request with a program like cURL. There is one test accessible via cURL that does not properly sanitize user input, allowing an attacker to execute shell commands as the root user."
# text = "Multiple stack-based buffer overflows in the RTF import feature in AbiWord before 2.2.11 allow user-assisted attackers to execute arbitrary code via an RTF file with long identifiers, which are not properly handled in the (1) ParseLevelText, (2) getCharsInsideBrace, (3) HandleLists, (4) or (5) HandleAbiLists functions in ie_imp_RTF.cpp, a different vulnerability than CVE-2005-2964."
# import xmlrpclib
# proxy = xmlrpclib.ServerProxy("http://10.141.209.137:28000/")
# origin_data =  proxy.parse(text)

def GenerateGraph(origin_data):
    edges = []
    DG = G=pgv.AGraph(directed=True)    

    for edge in origin_data:
        edges.append((edge[0], edge[2], edge[1]))
        DG.add_edge(edge[0], edge[2], label=edge[1])


    G.layout(prog='dot')
    DG.draw("dg.png")